<template>
  <div class="main">
    <div class="title">
      <div>借阅日志查看</div>
    </div>
    <div class="search-bar">
      <div class="label-input">
        <span>搜索内容：</span>
        <el-input v-model="query.queryName" style="width: 200px" />
      </div>
      <div class="label-input">
        <span>选择时间 ：</span>
        <el-date-picker
          v-model="time"
          type="daterange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="changeTime"
        />
      </div>
      <div class="label-input">
        <span>选择类型：</span>
        <el-select
          v-model="query.type"
          placeholder="请选择类型"
          style="width: 200px"
        >
          <el-option
            v-for="item in borrowTypeList"
            :key="item.id"
            :label="item.configLabel"
            :value="item.configValue"
          />
        </el-select>
      </div>
      <el-button class="search-btn" @click="getTabelData">查询</el-button>
      <el-button type="info" @click="reset">重置</el-button>
    </div>
    <div class="cut-line"></div>
    <el-table
      :data="tabelData"
      height="400"
      style="width: 100%"
      border
      v-loading="loading"
      :header-cell-style="headerStyle"
    >
      <el-table-column type="index" align="center" label="#" width="50" />
      <el-table-column
        prop="personName"
        align="center"
        label="读者"
        width="150"
      />
      <el-table-column
        prop="bookName"
        align="center"
        label="图书名称"
        width="200"
      />
      <el-table-column
        prop="type"
        align="center"
        label="操作类型"
        width="200"
      />
      <el-table-column prop="operateTime" align="center" label="操作时间" />
    </el-table>

    <div class="pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="tabelDataTotal"
        :page-size="query.pageSize"
        :current-page="query.pageNum"
        @update:current-page="currentPageChange"
      />
    </div>
  </div>
</template>
  <script setup>
import { ref, reactive, onMounted } from "vue";
import { getBorrowLogApi } from "@/api/logApi.js";
import { getConfigDataApi } from "@/api/configApi.js";
import { DateFormatter } from "@/util/date.js";
import { useConfigStore } from "@/stores";
const configStore = new useConfigStore();
// 查询参数
const query = reactive({
  type: "",
  endTime: null,
  startTime: null,
  queryName: "",
  pageNum: 1,
  pageSize: 10,
});
const borrowTypeList = ref([]);
const time = ref([]);
const tabelDataTotal = ref(0);
const loading = ref(false);
const tabelData = ref([]);
// 表格头部样式
const headerStyle = () => {
  return {
    backgroundColor: "#a0cfff",
    color: "#000000",
    padding: "8px 0px",
  };
};
// 时间选择
const changeTime = (date) => {
  query.startTime = DateFormatter(date[0]);
  query.endTime = DateFormatter(date[1]);
};
// 当前页改变
const currentPageChange = (num) => {
  query.pageNum = num;
  getTabelData();
};
// 获取配置列表
const getTabelData = async () => {
  loading.value = true;
  let res = await getBorrowLogApi(query);
  tabelDataTotal.value = res.data.total;
  tabelData.value = res.data.rows;
  loading.value = false;
};
// 重置
const reset = () => {
  Object.assign(query, {
    endTime: null,
    startTime: null,
    queryName: "",
    type: "",
    pageNum: 1,
    pageSize: 10,
  });
  getTabelData();
};
onMounted(async () => {
  getTabelData();
  if (!configStore.borrowTypeList) {
    let res = await getConfigDataApi({
      configKey: "system:borrow:type",
    });
    configStore.borrowTypeList = res.data;
    borrowTypeList.value = res.data;
  } else {
    borrowTypeList.value = configStore.borrowTypeList;
  }
  console.log(borrowTypeList.value);
});
</script>
<style lang="scss" scoped>
.main {
  width: 100%;
  height: 100%;
  background-color: #ffffff;
  padding: 10px 20px;
  .title {
    font-size: 20px;
    font-weight: 600;
    display: flex;
    justify-content: space-between;
  }
}
.search-bar {
  margin-top: 20px;
  display: flex;
  align-items: center;
  height: 30px;
  .search-btn {
    background-color: #79bbff;
    color: #ffffff;
  }
  .el-button {
    height: 28px;
  }
}
.label-input {
  display: flex;
  color: #aaaaaa;
  height: 30px;
  align-items: center;
  margin-right: 20px;
  font-weight: 550;
  .el-input {
    border: none;
    height: 30px;
  }
}
.cut-line {
  margin-top: 30px;
  height: 1px;
  background-color: #cdd0d6;
}

.pagination {
  display: flex;
  justify-content: center;

  .el-pagination {
    margin: auto 0;
    margin-top: 10px;
  }
}
:deep(.el-table) {
  margin-top: 20px;
  .el-table__cell {
    padding: 4px;
  }
  .cell {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
</style>